package com.example.gulimallproduct.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.common.utils.R;
import com.example.gulimallproduct.entity.SpuInfoEntity;
import com.example.gulimallproduct.request.SelectSpuOrSkuInfoPageRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.utils.PageUtils;
import com.example.common.utils.Query;

import com.example.gulimallproduct.dao.SkuInfoDao;
import com.example.gulimallproduct.entity.SkuInfoEntity;
import com.example.gulimallproduct.service.SkuInfoService;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public R selectSkuInfo(SelectSpuOrSkuInfoPageRequest request) {
        Map<String, Object> params=new HashMap<>();
        params.put("page", request.getPage().toString());
        params.put("limit", request.getLimit().toString());
        LambdaQueryWrapper<SkuInfoEntity> queryWrapper = new LambdaQueryWrapper<SkuInfoEntity>()
                .eq(Objects.nonNull(request.getCatelogId())&&request.getCatelogId()>0, SkuInfoEntity::getCatalogId, request.getCatelogId())
                .eq(Objects.nonNull(request.getBrandId())&&request.getBrandId()>0, SkuInfoEntity::getBrandId, request.getBrandId())
                .and(StringUtils.isNotBlank(request.getKey()),
                        wrapper -> wrapper.likeRight(SkuInfoEntity::getSkuName, request.getKey())
                                .or().likeRight(SkuInfoEntity::getSkuDesc, request.getKey())
                                .or().likeRight(SkuInfoEntity::getSkuTitle, request.getKey())
                                .or().likeRight(SkuInfoEntity::getSkuSubtitle, request.getKey())
                )
                .ge(Objects.nonNull(request.getMin())&&request.getMin().compareTo(BigDecimal.ZERO)>0, SkuInfoEntity::getPrice, request.getMin())
                .le(Objects.nonNull(request.getMax())&&request.getMax().compareTo(BigDecimal.ZERO)>0, SkuInfoEntity::getPrice, request.getMax());



        // 只有当排序字段不为空时才添加排序条件
        if (StringUtils.isNotBlank(request.getSidx())) {
            String order = StringUtils.isNotBlank(request.getOrder()) ? request.getOrder() : "ASC";
            queryWrapper.last("order by " + request.getSidx() + " " + order);
        }
        IPage<SkuInfoEntity> page = page(new Query<SkuInfoEntity>().getPage(params), queryWrapper);
        PageUtils pageUtils = new PageUtils(page);
        return R.ok().put("page", pageUtils);
    }

}